######################
#  Replication code for 'Mediating the Electoral Connection', forthcoming in the JOP
#  John Henderson and John Brooks
#  12/7/2015    
######################    

# tableX-imputeghk.R
#  :: runs iv analysis to produce results for table X using gomez, hansford and krause rain measures, after 
#   imputing all missing data

# ghk rain measure robust

rm(list=ls())
setwd('~/Dropbox/rainReplication')

if(length(which(installed.packages()[,1]=='mice'))!=1){
	install.packages('mice')                        
}     
library(mice)     

fes.type=3
non.missings=4 

ghk=T

source('prelimRobust.R')
             
rain_ghk=covs$rain_ghk
rain_ghk_prev=covs$rain_ghk_prev
rain_elec00=covs$rain_elec00
rain_elec00_prev=covs$rain_elec00_prev

covs=covs_all 
covs$rain_ghk=rain_ghk
covs$rain_ghk_prev=rain_ghk_prev
covs$rain_elec00=rain_elec00
covs$rain_elec00_prev=rain_elec00_prev
                
set.seed(1005)
m.covs=mice(as.matrix(covs[,-c(29)]),m=5) 
#m.covs=mice(covs[,c(1,2,30)],m=1)   
c.covs=list()    

for(i in 1:5){
	c.covs[[i]]=complete(m.covs,i)
}      
  
rr1=rowMeans(cbind(c.covs[[1]][,29],c.covs[[2]][,29],c.covs[[3]][,29],c.covs[[4]][,29],c.covs[[5]][,29]))
rr2=rowMeans(cbind(c.covs[[1]][,30],c.covs[[2]][,30],c.covs[[3]][,30],c.covs[[4]][,30],c.covs[[5]][,30]))
rr3=rowMeans(cbind(c.covs[[1]][,31],c.covs[[2]][,31],c.covs[[3]][,31],c.covs[[4]][,31],c.covs[[5]][,31]))
rr4=rowMeans(cbind(c.covs[[1]][,32],c.covs[[2]][,32],c.covs[[3]][,32],c.covs[[4]][,32],c.covs[[5]][,32]))

covs$rain_ghk=rr1
covs$rain_ghk_prev=rr2
covs$rain_elec00=rr3  
covs$rain_elec00_prev=rr4


#detach(covs)
# ANALYSIS             

main_iv1_fe=ivreg(vote~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose_prv + vote_prv + rain_ghk+rain_ghk_prev,
	subset=full,data=covs)	
	  
main_iv2_fe=ivreg(vote~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose + dose_prv + vote_prv,
	~as.factor(fe_id_num)+d_inc+
	dist_prev + midterm + pres_party + 
	black + construction + educ + 
	minc + farmer + forborn + gvtwkr + manuf + pop + unempld + 
	urban + retail + sos + gov + comp_cq + redistricted + 
	dose_prv + vote_prv + rain_elec00+rain_elec00_prev,
	subset=full,data=covs) 

main_iv1_fe_sum=summary(main_iv1_fe)
main_iv2_fe_sum=summary(main_iv2_fe)
                                  
main_iv1_fe_sumcl=coeftest.cluster(covs[full,],main_iv1_fe,cluster1='as.factor(fe_id_num)')
main_iv2_fe_sumcl=coeftest.cluster(covs[full,],main_iv2_fe,cluster1='as.factor(fe_id_num)')
      
save(
main_iv1_fe,main_iv2_fe,
main_iv1_fe_sum,main_iv2_fe_sum,
main_iv1_fe_sumcl,main_iv2_fe_sumcl,
file=paste('robustness/tableX_imputeghk-',non.missings,'_',fes.type,'.Rdata',sep=''))     

rm(
main_iv1_fe,main_iv2_fe,
main_iv1_fe_sum,main_iv2_fe_sum,
main_iv1_fe_sumcl,main_iv2_fe_sumcl
)              

# END      